<template>
  <div class="admin-order-management">
    <el-card>
      <template #header>
        <div class="card-header">
          <h2>订单管理</h2>
          <el-button type="primary" @click="handleAddOrder">新建订单</el-button>
        </div>
      </template>

      <!-- 搜索栏 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="订单编号">
          <el-input v-model="searchForm.order_number" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态">
            <el-option label="全部" value="" />
            <el-option label="待处理" value="pending" />
            <el-option label="配送中" value="delivering" />
            <el-option label="已完成" value="completed" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 订单列表 -->
      <el-table :data="orders" style="width: 100%">
        <el-table-column prop="order_number" label="订单编号" width="180" />
        <el-table-column prop="customer_name" label="客户名称" width="120" />
        <el-table-column prop="address" label="配送地址" min-width="200" />
        <el-table-column prop="priority" label="优先级" width="100">
          <template #default="{ row }">
            <el-tag :type="getPriorityType(row.priority)">
              {{ getPriorityText(row.priority) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" width="180" />
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
            <el-button type="success" link @click="handleViewDetails(row)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '新建订单' : '编辑订单'"
      width="50%"
    >
      <el-form :model="orderForm" label-width="100px">
        <el-form-item label="客户名称">
          <el-input v-model="orderForm.customer_name" />
        </el-form-item>
        <el-form-item label="配送地址">
          <el-input v-model="orderForm.address" type="textarea" />
        </el-form-item>
        <el-form-item label="优先级">
          <el-select v-model="orderForm.priority">
            <el-option label="高" value="high" />
            <el-option label="中" value="medium" />
            <el-option label="低" value="low" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="orderForm.status">
            <el-option label="待处理" value="pending" />
            <el-option label="配送中" value="delivering" />
            <el-option label="已完成" value="completed" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'

// 搜索表单
const searchForm = ref({
  order_number: '',
  status: ''
})

// 订单列表数据
const orders = ref([
  {
    id: 1,
    order_number: 'ORD20240301001',
    customer_name: '公司A',
    address: '北京市朝阳区建国路88号',
    created_at: '2024-03-01 09:00:00',
    priority: 'high',
    status: 'pending'
  },
  {
    id: 2,
    order_number: 'ORD20240301002',
    customer_name: '公司B',
    address: '北京市海淀区中关村大街1号',
    created_at: '2024-03-01 10:30:00',
    priority: 'medium',
    status: 'delivering'
  },
  {
    id: 3,
    order_number: 'ORD20240301003',
    customer_name: '公司C',
    address: '北京市西城区西单大悦城',
    created_at: '2024-03-01 11:15:00',
    priority: 'low',
    status: 'completed'
  }
])

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(3)

// 对话框相关
const dialogVisible = ref(false)
const dialogType = ref('add')
const orderForm = ref({
  customer_name: '',
  address: '',
  priority: 'medium',
  status: 'pending'
})

// 获取优先级样式
const getPriorityType = (priority) => {
  const typeMap = {
    high: 'danger',
    medium: 'warning',
    low: 'info'
  }
  return typeMap[priority] || 'info'
}

// 获取优先级文本
const getPriorityText = (priority) => {
  const textMap = {
    high: '高优先级',
    medium: '中优先级',
    low: '低优先级'
  }
  return textMap[priority] || '未知'
}

// 获取状态样式
const getStatusType = (status) => {
  const typeMap = {
    pending: 'warning',
    delivering: 'primary',
    completed: 'success'
  }
  return typeMap[status] || 'info'
}

// 获取状态文本
const getStatusText = (status) => {
  const textMap = {
    pending: '待处理',
    delivering: '配送中',
    completed: '已完成'
  }
  return textMap[status] || '未知'
}

// 处理搜索
const handleSearch = () => {
  console.log('搜索条件:', searchForm.value)
  // 这里实现搜索逻辑
}

// 处理重置
const handleReset = () => {
  searchForm.value = {
    order_number: '',
    status: ''
  }
}

// 处理新建订单
const handleAddOrder = () => {
  dialogType.value = 'add'
  orderForm.value = {
    customer_name: '',
    address: '',
    priority: 'medium',
    status: 'pending'
  }
  dialogVisible.value = true
}

// 处理编辑
const handleEdit = (row) => {
  dialogType.value = 'edit'
  orderForm.value = { ...row }
  dialogVisible.value = true
}

// 处理查看详情
const handleViewDetails = (row) => {
  console.log('查看订单详情:', row)
}

// 处理提交
const handleSubmit = () => {
  console.log('提交订单:', orderForm.value)
  ElMessage.success(dialogType.value === 'add' ? '新建成功' : '更新成功')
  dialogVisible.value = false
}

// 处理分页大小变化
const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

// 处理页码变化
const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}

onMounted(() => {
  // 这里可以加载实际数据
  console.log('订单管理页面加载完成')
})
</script>

<style scoped>
.admin-order-management {
  padding: 24px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 